/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.wx.gk.server;

import cn.ac.iie.di.commons.httpserver.framework.server.HttpServer;
import cn.ac.iie.di.wx.gk.common.RuntimeEnv;
import cn.ac.iie.di.wx.gk.configuration.Configuration;
import cn.ac.iie.di.wx.gk.handler.CmdAppealHandler;
import cn.ac.iie.di.wx.gk.handler.CmdCommitHandler;
import cn.ac.iie.di.wx.gk.handler.CmdQueryHandler;
import cn.ac.iie.di.wx.gk.handler.CmdRevokeHandler;
import java.util.Timer;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

/**
 *
 * @author Administrator
 */
public class Server {

    static HttpServer httpServer = null;
    static Logger logger = Logger.getLogger(Server.class.getName());

    public static void main(String[] args) {
        PropertyConfigurator.configure(ClassLoader.getSystemClassLoader().getResource("log4j.properties"));
        logger.info("log4j.properties has been configured");
        try {
            init();
            startup();
            logger.info("server has been started .");
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
        }
    }

    public static void init() throws Exception {
        String configurationFileName = "configuration.properties";
        Configuration conf = Configuration.getConfiguration(ClassLoader.getSystemClassLoader().getResource(configurationFileName).getPath());
        if (conf == null) {
            logger.error("reading " + configurationFileName + " is failed.");
        }
        try {
            RuntimeEnv.initialize(conf);///参数，
            logger.info("init successfully");
        } catch (Exception ex) {
            logger.error("initializng runtime enviroment is failed for" + ex.getMessage());
        }
    }

    public static void startup() {
        try {
            httpServer = HttpServer.getHttpServer(
                    (String) RuntimeEnv.getParam(RuntimeEnv.JETTY_SERVER),
                    Integer.parseInt((String) RuntimeEnv.getParam(RuntimeEnv.JETTY_PORT)),
                    Integer.parseInt((String) RuntimeEnv.getParam(RuntimeEnv.JETTY_THREADNUM)));
            httpServer.registerContext("/cmdControl");
            httpServer.registerContextHandler("/cmdControl", "/cmdCommit", CmdCommitHandler.class);//阅读数
            httpServer.registerContextHandler("/cmdControl", "/cmdQuery", CmdQueryHandler.class);//真实ID
            httpServer.registerContextHandler("/cmdControl", "/cmdAppeal", CmdAppealHandler.class);//粉丝数
            httpServer.registerContextHandler("/cmdControl", "/cmdRevoke", CmdRevokeHandler.class);//图片URL
            httpServer.startup();
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
        }

    }
}
